Decorating classes - sort instances by instantiation timeΒΆ
Decorating classes - sort instances by instantiation time.
Consider a feature of a class such that each instance knows when
it was instantiated, and instances are sorted by their creation times.
This has general applicability across many different classes,
and requires the addition of three attributes:
* the instantiation timestamp
* __gt__ method
* __lt__ method
import functools
import time
def sortable_by_creation_time(cls):
""" Given a class, augment the class to have its instances be sortable
by the timestamp at which they were instantiated.
"""
# Augment the class' original '__init__' method to also store a '_created' attribute
# on the instance, which corresponds to when it was instantiated
original_init = cls.__init__
@functools.wraps(original_init)
def new_init(self, *args, **kwargs):
original_init(self, *args, **kwargs)
self._created = time.time()
cls.__init__ = new_init
# Add '__lt__' and '__gt__' methods that return True or False
# based on the created values in question
cls.__lt__ = lambda self, other: self._created < other._created
cls.__gt__ = lambda self, other: self._created > other._created
# Done, return the class object
return cls
@sortable_by_creation_time
class Sortable(object):
""" Sortable Object """
def __init__(self, identifier):
self.identifier = identifier
def __repr__(self):
return self.identifier
first = Sortable('first')
time.sleep(0.1)
second = Sortable('second')
time.sleep(0.1)
third = Sortable('third')
time.sleep(0.1)
fourth = Sortable('fourth')
sortables = [fourth, second, first, third]
L = sorted(sortables)
# print(help(Sortable))
print(L)
# [first, second, third, fourth]